#include "stepdrv.h"

StepObiectDef YSO={0,0,0,0,0,0,0};
StepObiectDef XSO={0,0,0,0,0,0,0};

void YSO_Start(void)
{
	if(YSO.currentPulsePos == YSO.taragetPulsePos || YSO.stepStatus == BUSY)
	{
		return;
	}
	if(YSO.stepDelay<=1)
	{
		YSO.stepStatus = BUSY;        //电机忙碌状态
		YSO.stepDelay += YSO.stepDelayReload;
		if(YSO.currentPulsePos<YSO.taragetPulsePos)
		{
			//上
			YSO.nowDir=CCW;
		}
		else
		{
			//下
			YSO.nowDir=CW;
		}
		 HAL_GPIO_WritePin(TOP_DIR_GPIO_Port, TOP_DIR_Pin,YSO.nowDir);
		 //开启PWM中断
		 HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
		 //开启PWM
		 HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
	}
}

void XSO_Start(void)
{
	if(XSO.currentPulsePos == XSO.taragetPulsePos || XSO.stepStatus == BUSY)
	{
		return;
	}
	if(XSO.stepDelay<=1)
	{
		XSO.stepStatus = BUSY;        //电机忙碌状态
		XSO.stepDelay += XSO.stepDelayReload;

		if(XSO.currentPulsePos<XSO.taragetPulsePos)
		{
			//右
			XSO.nowDir=CW;
		}
		else
		{
			//左
			XSO.nowDir=CCW;
		}
		 HAL_GPIO_WritePin(BTM_DIR_GPIO_Port, BTM_DIR_Pin,XSO.nowDir);
		 //开启PWM中断
		 HAL_TIM_PWM_Start_IT(&htim4,TIM_CHANNEL_1);
		 //开启PWM
		 HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
	}
}

void XSO_End(void)
{
	if(YSO.stepDelay>1)
	{
		YSO.stepDelay--;
	}
	if(XSO.currentPulsePos <XSO.taragetPulsePos)
	{
		XSO.currentPulsePos ++;
	}
	else
	{
		XSO.currentPulsePos --;
	}
	XSO.stepStatus = FREE;     //电机空闲状态
	HAL_TIM_PWM_Stop(&htim4,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop_IT(&htim4,TIM_CHANNEL_1);
}

void YSO_End(void)
{
	if(XSO.stepDelay>1)
	{
		XSO.stepDelay--;
	}
	if(YSO.currentPulsePos <YSO.taragetPulsePos)
	{
		YSO.currentPulsePos ++;
	}
	else
	{
		YSO.currentPulsePos --;
	}
	YSO.stepStatus = FREE;     //电机空闲状态
	HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop_IT(&htim3,TIM_CHANNEL_1);
}

void locatesmooth(void)
{
	if(CO.workStatus == BUSY)
	{
		return;
	}

	if(CO.pointNum>1)//多点路线
	{
		if(CO.loop<0)//一直转
		{
			CO.pointindex = (++CO.pointindex)%CO.pointNum;
		}
		else if(CO.loop==0)
		{
			CO.pointindex = 0;
		}
		else
		{
			CO.pointindex = (++CO.pointindex)%CO.pointNum;
		}
		if(CO.pointindex==(CO.pointNum-1)&&CO.loop>0)
		{
			CO.loop--;
		}
	}
	else //单点路线
	{
		CO.pointindex = 0 ;
	}
	XSO.taragetPulsePos = (CO.targetPointer+CO.pointindex)->x;
	YSO.taragetPulsePos = (CO.targetPointer+CO.pointindex)->y;

	XSO.distanceLength = XSO.taragetPulsePos-XSO.currentPulsePos;
	YSO.distanceLength = YSO.taragetPulsePos-YSO.currentPulsePos;
	if(XSO.distanceLength==0 && YSO.distanceLength==0)
	{
		CO.workStatus = FREE;
		return;
	}
	CO.workStatus = BUSY;
	if(XSO.distanceLength==0 || YSO.distanceLength==0)//横平竖直，不存在同步运动
	{
		XSO.stepDelayReload = 0;
		YSO.stepDelayReload = 0;
	}
	else if(absint(XSO.distanceLength) > absint(YSO.distanceLength))//靠近x移动
	{
		XSO.stepDelayReload = 0;
		YSO.stepDelayReload = (absfloat(XSO.distanceLength)/absfloat(YSO.distanceLength));
	}
	else if(absint(XSO.distanceLength) < absint(YSO.distanceLength))//靠近y移动
	{
		XSO.stepDelayReload = (absfloat(YSO.distanceLength)/absfloat(XSO.distanceLength));
		YSO.stepDelayReload = 0;
	}
	else
	{
		XSO.stepDelayReload = 0;
		YSO.stepDelayReload = 0;
	}
}
